/* 
 *	Copyright (C) 2009-2010 miaohua
 *	http://code.google.com/p/lrcshower/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "StdAfx.h"
#include <string>
#include "Config.h"
#include "BaiduDecodeUrlHelperFunction.h"
#include "HelperFunctions.h"
using namespace std;

int Magic_K[1024];
int Magic_H[1024];

void Magic_N(int *K, int *H, int S, int P, int Q)
{
     for( int R=S;R<=P;R++)
     {
          K[R]=R+Q;
          H[R+Q]=R;
     }
}

tstring Magic_A(const tstring & Q, int Magic_M, int *K, int *H)
{
     int P=Q.size();
     tstring S;
     for( int R=0;R<P;R++)
     {
          TCHAR T=Q[R];
          if( (T >= 'A' && T <= 'Z') || (T >= 'a' && T <= 'z')  || (T >= '0' && T <= '9') )
          {
              int U = H[Q[R]]-Magic_M;
              if( U < 0 ){ U+=62; }
              T = TCHAR(K[U]);
          }
          S += T;
     }
     return S;
}


tstring  DecodeBaiduUrl(int MagicNum, tstring & EncryptUrl1, tstring & EncryptUrl2)
{
     Magic_N(Magic_K, Magic_H, 0, 9, 48);
     Magic_N(Magic_K, Magic_H, 10, 35, 55);
     Magic_N(Magic_K, Magic_H, 36, 61, 61);
     int F=MagicNum;
     int M = F%26;
     M = M ? M :1;

     tstring O = Magic_A(EncryptUrl1, M, Magic_K, Magic_H);
     tstring E = (EncryptUrl1==EncryptUrl2) ? O : Magic_A(EncryptUrl2, M, Magic_K, Magic_H);

     return E;
}

           
int asc_arr1[128];
int asc_arr2[128];

void InitAscArr(int Head, int Bottom, int Middle)
 {
     for( int i = Head; i <= Bottom; i++ ) 
     {
          asc_arr1[i] = i + Middle;
          asc_arr2[i + Middle] = i;
     }
 }


tstring BaiduMp3DownloadUrlDecode(const tstring & Url, unsigned int MagicNum)
 {
     int len = Url.size();
     tstring Decurl;

     unsigned int key = MagicNum % 26;
     key = key ? key : 1;

     InitAscArr(0, 9, 48);
     InitAscArr(10, 35, 55);
     InitAscArr(36, 61, 61);

     for ( int  i = 0; i < len; ++i ) 
     {
           TCHAR Word = Url[i];
           if( (Word >='0' && Word <= '9') || 
               (Word >='a' && Word <= 'z') || 
               (Word >='A' && Word <= 'Z') )
           {
               int pos = asc_arr2[Url[i]] - key;
               if ( pos < 0 )
                    pos += 62;
               Word = asc_arr1[pos];
           }
           Decurl += Word;
     }

     return Decurl;
}

tstring BaiduMp3UrlGen(const tstring & SongName, int Page, int SongPerPage)
{
     return UrlGenHelper(Baidu_Music_Url, SongName, Page*SongPerPage);
}

tstring BaiduLrcUrlGen(const tstring & Song, const tstring & Singer, int Page, int SongPerPage)
{
     return UrlGenHelper(Baidu_Lrc_Url, Song + _T("+") + Singer, Page*SongPerPage);
}